<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('taskList.equipmentRepairReport')" @clickRight="clickRight" scanType="1" :rightType="1"></nav-bar-custom>
		</template>

		<template v-slot:gBody>
			<view class="barcodeSplitting">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('fgReturn.department') }}：</text>
						<comboGrid ref="comboGridRef" v-model="data.department"
							:requestParam="data.requestParamDepartment"
							requestUrl="/digital/notoken/repair/selectDepartment"
							:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="label"
							:column="data.columnRuleDepartment.column" bindValue="label" @setClick="departmentSelect">
						</comboGrid>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('equipmentRepair.processArea') }}：</text>
						<comboGrid ref="comboGridRef2" v-model="data.process"
							:requestParam="data.requestParamProcess" requestUrl="/digital/notoken/repair/selectProcess"
							:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="label"
							:column="data.columnRuleProcess.column" bindValue="label" @setClick="processSelect">
						</comboGrid>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('equipmentRepair.machineNumber') }}：</text>
						<superweiCombox style="text-align: right;" :candidates="data.machineList"
							:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="label"
							v-model="data.machine" @select="machineSelect"></superweiCombox>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('equipmentRepair.faultType') }}：</text>
						<superweiCombox style="text-align: right;" :candidates="data.faultTypeList"
							:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="label"
							v-model="data.faultType" @select="faultTypeSelect"></superweiCombox>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('equipmentRepair.equipmentName') }}：</text>
						<superweiCombox style="text-align: right;" :candidates="data.equipmentNameList"
							:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="equipmentNameI18"
							v-model="data.equipmentName" @select="equipmentNameSelect"></superweiCombox>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 15rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('equipmentRepair.severityLevel') }}：</text>
						<superweiCombox style="text-align: right;" :candidates="data.severityLevelList"
							:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="label"
							v-model="data.severityLevel" @select="severityLevelSelect"></superweiCombox>
					</view>
					<view class="custom-card-itemTwo cradItem" style="padding: 80rpx 0; ">
						<text class="item-info-title"><text class="required-color">*</text>{{$t('months.FaultDescription') }}：</text>
						<uni-easyinput class="uni-mt-5"  v-model="data.faultDesc" type="textarea" 
						:auto-height="true" ></uni-easyinput>
					</view>
				</view>
				<!-- 底部确定按钮 -->
				<view class="page-bottom-box bottom-postion">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="handelSubmit">
						{{ $t('calendar.submit') }}</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>

<script setup>
//引入文件
import { ref, reactive, onMounted, computed, watch, toRaw } from "vue";
import { selectDepartment, selectMachinet, selectFaultType, selectEquipment, selectPriority, addOrderEms } from '@/api/api.js'
import pdaScan from '@/hooks/pdaScan.js'
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_COMMIT, OPERATION_CHOOSE,USERNAME } from '@/constant/index.js'
import { useStore } from "vuex";
import storageBin from '@/components/storageBin.vue';
import comboGrid from "@/components/comboGrid2";
import superweiCombox from '@/components/superweiCombox';
import spChooseBox from '@/components/spChooseBox';
import chooseSelect from '@/components/chooseSelect';

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

const { scan } = pdaScan()
const comboGridRef = ref(null)
const comboGridRef2 = ref(null)

let store = useStore();

//注册组件
components: {
	storageBin,
		comboGrid,
		spChooseBox,
		chooseSelect,
		superweiCombox
}

const data = reactive({
	department: '',
	process: "",
	machine: "",
	faultDesc:'',
	faultType: "",
	equipmentName: '',
	severityLevel: "",
	// code
	deportmentCode: '',
	faultTypeCode: "",
	processCode: "",
	machineCode: "",
	equipmentNameCode: "",
	severityLevelCode: "",
	// 存数据
	requestParamDepartment: {},
	columnRuleDepartment: {
		column: [{
			label: t('fgReturn.department'),
			name: "label",
		}]
	},
	requestParamProcess: {},
	columnRuleProcess: {
		column: [{
			label: t('equipmentRepair.processArea'),
			name: "label",
		}]
	},
	machineList: [],
	faultTypeList: [],
	equipmentNameList: [],
	severityLevelList: [],
})

onMounted(() => {
	// 获取部门，给默认值第一条
	selectDepartment().then(res => {
		if (res.status == 200) {
			data.department = res.data[0].label
			data.deportmentCode = res.data[0].value
		}
	})
})

// 部门选择
const departmentSelect = (e) => {
	data.department = e.label
	data.deportmentCode = e.value
	data.process = ''
	data.processCode = ''
	data.machine =  ''
	data.machineCode =  ''
	data.faultType = ''
	data.faultTypeCode =''
	data.equipmentName = ''
	data.equipmentNameCode = ''
	data.faultTypeList=[]
	data.equipmentNameList=[]
	data.severityLevelList=[]

}

// 工序选择
const processSelect = (e) => {
	data.process = e.label
	data.processCode = e.value
	data.machine =  ''
	data.machineCode =  ''
	data.faultType = ''
	data.faultTypeCode =''
	data.equipmentName = ''
	data.equipmentNameCode = ''
	data.faultTypeList=[]
	data.equipmentNameList=[]
	data.severityLevelList=[]
	// 获取工序下的机台号
	selectMachinet({
		department: data.deportmentCode,
		processArea: data.processCode
	}).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '设备报修-机台号'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.machineList = res.data
		}
	})

}
// 机台号
const machineSelect = (e) => {
	data.machine = e.label
	data.machineCode = e.value
	data.faultType = ''
	data.faultTypeCode =''
	data.equipmentName = ''
	data.equipmentNameCode = ''
	data.faultTypeList=[]
	data.equipmentNameList=[]
	data.severityLevelList=[]

	// 故障类型选择
	selectFaultType().then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '设备报修-故障类型选择'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.faultTypeList = res.data
		}
	})
}
// 故障类型选择
const faultTypeSelect = (e) => {
	data.faultType = e.label
	data.faultTypeCode = e.value
	data.equipmentName = ''
	data.equipmentNameCode = ''

	// 故障设备名称
	selectEquipment({
		department: data.deportmentCode,
		processArea: data.processCode,
		machineNo: data.machineCode
	}).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '设备报修-故障设备名称'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.equipmentNameList = res.data
		}
	})
}
// 故障设备名称
const equipmentNameSelect = (e) => {
	data.equipmentName = e.equipmentNameI18
	data.equipmentNameCode = e.equipmentName

	// 故障严重程度
	selectPriority().then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '设备报修-故障严重程度'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_CHOOSE
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			data.severityLevelList = res.data
		}
	})

}

// 故障严重程度
const severityLevelSelect = (e) => {
	data.severityLevel = e.text
	data.severityLevelCode = e.value
}

const times = () => {
	// 获取当前时间
	var current = new Date()
	let year = current.getFullYear();
    let month = ('0' + (current.getMonth() + 1)).slice(-2); // 月份是从0开始的，所以要加1，并且要保证是两位数
    let day = ('0' + current.getDate()).slice(-2); // 同理，日期也要保证是两位数
    let hours = ('0' + current.getHours()).slice(-2); // 小时也要保证是两位数
    let minutes = ('0' + current.getMinutes()).slice(-2); // 分钟也要保证是两位数
    let seconds = ('0' + current.getSeconds()).slice(-2); // 秒数也要保证是两位数
    let formatted= year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
	// 返回数据
	return formatted
}
// 提交
const handelSubmit = () => {
	if (!data.deportmentCode) {
		uni.showToast({ title: t('fgReturn.chooseDepartment'), icon: "none" });
		return
	}
	if (!data.processCode) {
		uni.showToast({ title: t('equipmentRepair.selectProcessArea'), icon: "none" });
		return
	}
	if (!data.machineCode) {
		uni.showToast({ title: t('equipmentRepair.selectMachineNumber'), icon: "none" });
		return
	}
	if (!data.faultTypeCode) {
		uni.showToast({ title: t('equipmentRepair.selectTypeFault'), icon: "none" });
		return
	}
	if (!data.equipmentNameCode) {
		uni.showToast({ title: t('equipmentRepair.selectFaultName'), icon: "none" });
		return
	}
	if (!data.severityLevelCode) {
		uni.showToast({ title: t('equipmentRepair.selectSeverity'), icon: "none" });
		return
	}
	// 添加加载框
	uni.showLoading({
		title: 'loading',
		mask: true
	});
	addOrderEms({
		department: data.deportmentCode,
		processArea: data.processCode,
		machineNo: data.machineCode,
		faultType: data.faultTypeCode,
		faultDevice: data.equipmentNameCode,
		faultLevel: data.severityLevelCode,
		reporterId:uni.getStorageSync(USERNAME),
		faultDesc: data.faultDesc,
		reportTime: times()
	}).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '设备报修'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.status,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_COMMIT
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.status == 200) {
			//隐藏加载框
			uni.hideLoading();
			uni.showToast({ title: t('smashRemoval.submitSuccessfully'), icon: "none" });
			data.severityLevel = ''
			data.severityLevelCode =''
			data.process = ''
			data.processCode = ''
			data.machine = ''
			data.machineCode = ''
			data.faultType = ''
			data.faultTypeCode = ''
			data.equipmentName = ''
			data.equipmentNameCode = ''
			
			uni.redirectTo({
				url: '/pages/equipment/equipment'
			});
		}else{
			uni.showToast({ title: res.message, icon: "none" });
		}
	})
}

// 历史记录
const clickRight=()=>{
	uni.navigateTo({
		url: '/pages/equipment/equipmentHistoryRecord'
	});
}
</script>

<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}
</style>
